package com.tdkj.action.front.member.risevalue;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.financial.BeanFinancialAccount;
import com.tdkj.model.friendlylink.BeanFriendlylink;
import com.tdkj.model.risevalue.BeanAdvertAuctionBidInfo;
import com.tdkj.model.risevalue.BeanAdvertAuctionInfo;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.ServiceException;
import com.tdkj.service.risevalue.IBeanAdvertAuctionService;
public class AdvertAuctionAction extends BaseAction {
	private Long id;
	private Long bid;
	private int price;//出价
	private String[] bids;
	private int currentprice;//当前最高价格
	private String fileurl;
	private BeanAdvertAuctionInfo auctioninfo;
	private List<BeanAdvertAuctionInfo> auctioninfos=new ArrayList<BeanAdvertAuctionInfo>();
	private IBeanAdvertAuctionService advertAuctionService;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getBid() {
		return bid;
	}
	public void setBid(Long bid) {
		this.bid = bid;
	}
	public String getFileurl() {
		return fileurl;
	}
	public void setFileurl(String fileurl) {
		this.fileurl = fileurl;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public String[] getBids() {
		return bids;
	}
	public void setBids(String[] bids) {
		this.bids = bids;
	}
	public int getCurrentprice() {
		return currentprice;
	}
	public void setCurrentprice(int currentprice) {
		this.currentprice = currentprice;
	}
	public BeanAdvertAuctionInfo getAuctioninfo() {
		return auctioninfo;
	}
	public void setAuctioninfo(BeanAdvertAuctionInfo auctioninfo) {
		this.auctioninfo = auctioninfo;
	}
	public List<BeanAdvertAuctionInfo> getAuctioninfos() {
		return auctioninfos;
	}
	public void setAuctioninfos(List<BeanAdvertAuctionInfo> auctioninfos) {
		this.auctioninfos = auctioninfos;
	}
	public IBeanAdvertAuctionService getAdvertAuctionService() {
		return advertAuctionService;
	}
	@Resource
	public void setAdvertAuctionService(
			IBeanAdvertAuctionService advertAuctionService) {
		this.advertAuctionService = advertAuctionService;
	}
	public String findList(){
		Criteria criteria = this.createCriteria(BeanAdvertAuctionInfo.class);
		criteria.add(Restrictions.eq("status", "开始竞价"));
		criteria.addOrder(Order.desc("astarttime"));// 对状态进行排序
		this.queryByPage(criteria, "advertAuctionAction!findList.action",30);
		return "list";
	}
	public String findAuctionBid(){
		auctioninfo=advertAuctionService.findById(this.getId(), BeanAdvertAuctionInfo.class);
		List<BeanAdvertAuctionBidInfo> binfos=auctioninfo.getAuctionbidinfos();
		if(binfos.size()>=1){
			Criteria criteria = this.createCriteria(BeanAdvertAuctionBidInfo.class);
			criteria.setProjection(Projections.projectionList().add(Projections.max("price")))
			.add(Restrictions.eq("auctioninfo", auctioninfo));
			currentprice=(Integer) criteria.list().get(0);
		}else{
			currentprice=auctioninfo.getStartprice();
		}
		Criteria criteria = this.createCriteria(BeanAdvertAuctionBidInfo.class);
		criteria.addOrder(Order.desc("auctiontime"));// 对状态进行排序
		criteria.add(Restrictions.eq("auctioninfo", auctioninfo));
		this.queryByPage(criteria, "advertAuctionAction!findAuctionBid.action", 4);
		return "auction_bid_add";
	}
	
	//根据竞价规则出价
	public String addAuctionBid(){
		BeanAdvertAuctionInfo info=advertAuctionService.findById(this.getId(), BeanAdvertAuctionInfo.class);
		//判断出价是否在时间间隔内
		boolean isInInterval=isInInterval(info);
		if(!isInInterval){
			this.addFieldError("intervalVlidate", "出价太频繁，请在时间出价间隔后出价!");
		}else{
			//判断是否参与过竞价，如果参与，返还冻结资金
			try {
				advertAuctionService.createBid(this.getCurrentUser().getId(), info.getId(), new Integer(this.getPrice()));
			} catch (ServiceException e) {
				int money=findAccount(info.getCurrency());
				//当前最高出价(当前最高价格+竞价系数的最低出价金额)
				int currentprice=this.getCurrentprice()+info.getAuctionrule().getLow();
				if(money<=currentprice) {
					this.addFieldError("priceVlidate", e.getMessage());
				}
				e.printStackTrace();
			}
		}
		id=this.getId();
		return findAuctionBid();
	}
	//获取账户余额
	private int findAccount(String currency){
		BeanFinancialAccount c=this.getCurrentUser().getFinancialAccount();
		if(currency.equals("银币")){
			//获取银币账户
			return c.getSilver();
		}else{
			//获取金币账户
			return c.getGold();
		}
	}
	//判断是否是在时间间隔
	private boolean isInInterval(BeanAdvertAuctionInfo a){
		String unit=a.getUnit();
		int timeinterval=a.getTimeinterval();
		int times=0;
		if(unit.equals("分钟")){
			times=timeinterval*1000*60;
		}else{
			times=timeinterval*1000*60*60;
		}
		BeanUser u=this.getCurrentUser();
		String hql = " select max(t1.auctiontime) from "+BeanAdvertAuctionBidInfo.class.getName()+" t1,"+
					 " "+BeanAdvertAuctionInfo.class.getName()+" t2"+
			         " where t2.id=t1.auctioninfo.id and t1.user.id="+u.getId();
		Query query = this.createQuery(hql);
		Timestamp t=(Timestamp)query.uniqueResult();
		if(t==null){
			return true;
		}else{
			Timestamp ct=new Timestamp(System.currentTimeMillis());
			if((ct.getTime()-t.getTime())<times){
				return false;
			}else{
				return true;
			}
		}
	}
	//获取竞价历史
	public String findAuctionHistory(){
		BeanUser u=this.getCurrentUser();
		Criteria criteria = this.createCriteria(BeanAdvertAuctionBidInfo.class);
		criteria.add(Restrictions.eq("user", u));
		criteria.addOrder(Order.desc("auctiontime"));// 对状态进行排序
		this.queryByPage(criteria, "advertAuctionAction!findAuctionHistory.action", 4);
		return "au_history";
	}
	//获取竞价成功的
	public String findAuctionSuccess(){
		BeanUser u=this.getCurrentUser();
		Criteria criteria = this.createCriteria(BeanAdvertAuctionInfo.class);
		criteria.add(Restrictions.eq("user", u));
		criteria.addOrder(Order.desc("auctiontime"));// 对状态进行排序
		this.queryByPage(criteria, "advertAuctionAction!findAuctionSuccess.action", 4);
		return "au_success";
	}
	//删除出价历史
	public String deleteAllBid() {
		if (null != bids) {
			for (String idtemp : bids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanAdvertAuctionBidInfo.class);
			}
		}
		return findAuctionHistory();
	}
	//删除出价历史
	public String deleteBid() {
		this.getBaseService().delete(this.getBid(), BeanAdvertAuctionBidInfo.class);
		return findAuctionHistory();
	}
	//获取广告位编辑界面
	public String findFileurlEdit(){
		auctioninfo=this.getBaseService().findById(this.getId(), BeanAdvertAuctionInfo.class);
		return "file_edit";
	}
	//编辑广告位
	public String editFileurl(){
		id=this.getId();
		auctioninfo=this.getBaseService().findById(this.getId(), BeanAdvertAuctionInfo.class);
		auctioninfo.setFileurl(this.getFileurl());
		this.getBaseService().saveOrUpdate(auctioninfo, BeanAdvertAuctionInfo.class);
		return findFileurlEdit();
	}
}
